//人名币数字转成大写
//2081320.34 的转换结果是 贰百零拾捌万壹千叁百贰拾零元叁角肆分  中间的零拾万之类的是否需要修改?
function toRmb(n) {
     var strOutput = "";
     var strUnit = "千百拾亿千百拾万千百拾元角分";
     n += "00";
     var intPos = n.indexOf(".");
     if (intPos >= 0)
         n = n.substring(0, intPos) + n.substr(intPos + 1, 2);
     strUnit = strUnit.substr(strUnit.length - n.length);
     for (var i=0; i < n.length; i++)
         strOutput += "零壹贰叁肆伍陆柒捌玖".substr(n.substr(i,1),1) + strUnit.substr(i,1);
     return strOutput;
}

function init() {
	configSystem();
	prepareData();
	configEvent();
	startUp();
}

function configSystem(){
	setDatePicker();
}

function prepareData(){
	prepareCustomerInfo();
	prepareProductInfo();
	prepareEntrepotInfo();
}

function configEvent() {
		$("#brand_img").draggable();
		$("#company_name").draggable();
		$("#order_detail").draggable();
		$("#buyer").draggable();
		$("#footer").draggable();
		$("#header").draggable();
		$("#a").draggable();	
				
		 $("#order_date").datepicker();
		 
		
		$("select[name|=customerName]").change(function(){
			var selected = customer[$(this).val()];
			$("input[name|=taxNo]").val(selected.taxNo);
			$("input[name|=address]").val(selected.address);
			$("input[name|=phone]").val(selected.phone);
			$("input[name|=bank]").val(selected.bank);
			$("input[name|=taxNo]").val(selected.taxNo);
			$("input[name|=accountNo]").val(selected.accountNo);		
		});
		
		$("input[name|=weight]").change(caculatePrice);
		$("input[name|=price]").change(caculatePrice);
	
}

function startUp() {
	$("select[name|=customerName]").change();
	caculatePrice();
}


function setDatePicker() {
		$.datepicker.setDefaults( {
		  closeText: '关闭',
		  prevText: '&#x3c;上月',
		  nextText: '下月&#x3e;',
		  currentText: '今天',
		  monthNames: ['一月','二月','三月','四月','五月','六月',
		  '七月','八月','九月','十月','十一月','十二月'],
		  monthNamesShort: ['一','二','三','四','五','六',
		  '七','八','九','十','十一','十二'],
		  dayNames: ['星期日','星期一','星期二','星期三','星期四','星期五','星期六'],
		  dayNamesShort: ['周日','周一','周二','周三','周四','周五','周六'],
		  dayNamesMin: ['日','一','二','三','四','五','六'],
		  weekHeader: '周',
		  dateFormat: 'yy年mm月dd日',
		  firstDay: 1,
		  isRTL: false,
		  showMonthAfterYear: true,
		  yearSuffix: '年'});
}

function caculatePrice() {
	var total = 0;
	$.each($("input[name|=subTotal]"), function(i,n){
		var sub_total = $($("input[name|=weight]")[i]).val() * $($("input[name|=price]")[i]).val();
		$(n).val(sub_total);
		total = total + sub_total;
	});
	$("input[name|=total]").val(total);
	$("input[name|=totalRmb]").val(toRmb(total));
}